import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import $td from "@/components/td/";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        redirect: "/admin/",
    },
    {
        path: "/admin/login",
        name: "admin-login",
        component: () => import("../views/admin/login/index.vue"),
        meta: {
            title: "管理员登录",
        },
    },
        {
        path: "/admin/index",
        name: "index-index",
        component: () => import("../views/admin/index/index.vue"),
        meta: {
            title: "管理员登录",
        },
    },
    {
        path: "/admin",
        name: "admin",
        component: () => import("../views/admin/index.vue"),
        meta: {
            title: "管理后台",
        },
        children: [
            {
                path: "/admin/example",
                name: "index",
                component: () => import("../views/admin/example/index.vue"),
                meta: {
                    title: "示例",
                },
            },
        ],
    },
    {
        path: "/404",
        name: "404",
        component: () => import("../views/404/index.vue"),
        meta: {
            title: "404",
        },
    },
    //没有匹配到的路由重定向到404
    {
        path: "/:pathMatch(.*)",
        redirect: "/404",
    },
];

const router = createRouter({
    history: createWebHistory("/"),
    routes,
});

router.beforeEach((to, from, next) => {
    // // 先设置标题为“加载中...”
    console.log(to.path);
    if (to.path == '' || to.path === "/" || to.path === "/admin/") {
        next({path: "/admin/example"});
    }
    //如果要去的路由以/admin开头 并且没有Token 跳转到/admin/login
    // if (to.path.includes("/admin") && !$td.getToken() && to.path !== "/admin/login") {
    //     next({path: "/admin/login"});
    // }

    document.title = "加载中...";
    document.title = to.meta.title + "";
    next();
});

export default router;
